package com.wyx.exceptionhandler;

import com.wyx.utils.CommonResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler {

    //指定出现什么异常执行这个方法
    @ExceptionHandler(Exception.class)
    @ResponseBody //为了返回数据
    public CommonResult error(Exception e) {
        log.error("处理异常");
        e.printStackTrace();
        return CommonResult.Error();
    }

    //特定异常
    @ExceptionHandler(ArithmeticException.class)
    @ResponseBody //为了返回数据
    public CommonResult error(ArithmeticException e) {
        log.error("算术处理异常");
        e.printStackTrace();
        return CommonResult.Error();
    }

    //自定义异常
    @ExceptionHandler(MyException.class)
    @ResponseBody //为了返回数据
    public CommonResult error(MyException e) {
        log.error(e.getMessage());
        e.printStackTrace();
        return CommonResult.Error();
    }

}
